//
//  EPLoginViewController.m
//  Epweike_Witkey
//
//  Created by Mac on 15/8/3.
//  Copyright (c) 2015年 zengxusheng. All rights reserved.
//

#import "EPLoginViewController.h"
#import "RegisterViewController.h"

@interface EPLoginViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UITextField *loginUsername;
@property (strong, nonatomic) IBOutlet UITextField *loginPassword;
@property (strong, nonatomic) IBOutlet UIButton *rememberPsw;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UILabel *hotPhoneLab;//热线电话

@end

@implementation EPLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self createLeftBarButtonImgName:@"login_close" highlightImgName:@"login_close"];
//    [self createRightBarButtonTitle:@"注册"];
    self.title = @"登录";
    
    _loginUsername.superview.layer.cornerRadius = 20;
    _loginUsername.superview.clipsToBounds = YES;
    _loginPassword.superview.layer.cornerRadius = 20;
    _loginPassword.superview.clipsToBounds = YES;
    _loginBtn.layer.cornerRadius = 20;
    //用户名
    _loginUsername.text = [UserDefaults objectForKey:kRememberuserNmae];
    
    //是否记住密码
    NSString *pswStr = [UserDefaults objectForKey:kRememberPSW];
    if (pswStr.length > 0)
    {
        _loginPassword.text = pswStr;
        _rememberPsw.selected = YES;
    }
    else
    {
        _loginPassword.text = @"";
        _rememberPsw.selected = NO;
    }
    
    //点击空白处，关闭键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textFieldResignFirstResponder)];
    [_scrollView addGestureRecognizer:tap];
}

/*
 *  关闭键盘
 */
- (void)textFieldResignFirstResponder
{
    for (UIWindow *w in [[UIApplication sharedApplication] windows])
    {
        [w endEditing:YES];
    }
}

//关闭登录界面
- (void)leftBarButtonClick:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

//注册
- (void)rightBarButtonClick:(UIButton *)btn
{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//记住密码
- (IBAction)rememberPswAction:(UIButton *)sender {
    _rememberPsw.selected = !sender.selected;
}
//立即注册
- (IBAction)registerAction:(id)sender {
    RegisterViewController *ctr = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:ctr animated:YES];
}

//登录
- (IBAction)loginAction:(UIButton *)sender {
    
    [self textFieldResignFirstResponder];
    
    if (_loginUsername.text.trim.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号或用户名"];
        return;
    }
    
    if (_loginPassword.text.trim.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    
    
    NSDictionary *params = [[NSDictionary alloc]init];
    params = @{@"userName":_loginUsername.text,@"password":_loginPassword.text};
    [self requestLoginWithParams:params];

}

- (IBAction)textFieldEditingChanged:(UITextField *)sender {
    if (sender == _loginUsername) {
        NSString *nameStr = [UserDefaults objectForKey:kRememberuserNmae];
        NSString *pswStr = [UserDefaults objectForKey:kRememberPSW];
        if ([sender.text isEqualToString:nameStr] && pswStr.length > 0)
        {
            _loginPassword.text = pswStr;
            _rememberPsw.selected = YES;
        }
        else
        {
            _loginPassword.text = @"";
            _rememberPsw.selected = NO;
        }
    }
}

#pragma mark - RequestAction 登录
-(void)requestLoginWithParams:(NSDictionary *)pramas
{
    [SVProgressHUD show];
    [[AFNetApiClient sharedClient] requestLoginWithParams:pramas success:^(AFHTTPRequestOperation *operation , id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = responseObject;
        if ([dic[@"responseCode"] intValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
            
            NSDictionary *userDic = dic[@"responseObj"];
            [UserDefaults setObject:userDic forKey:kUserInfo];
            [UserDefaults setBool:YES forKey:kDidLogin];
            [UserDefaults synchronize];
            
            if (_rememberPsw.selected == YES) {
                [UserDefaults setObject:pramas[@"userName"] forKey:kRememberuserNmae];
                [UserDefaults setObject:pramas[@"password"] forKey:kRememberPSW];
            }else
            {
                [UserDefaults setObject:@"" forKey:kRememberuserNmae];
                [UserDefaults setObject:@"" forKey:kRememberPSW];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:RefreshUserData object:dic[@"responseObj"]];
            [self leftBarButtonClick:nil];
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(AFHTTPRequestOperation *operation , NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
    
}



@end
